package com.sanyang.pda.ui.takestock;

import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.SelectBean;
import com.sanyang.pda.bean.TakeStockBean;
import com.sanyang.pda.contract.ConfirmStockContract;
import com.sanyang.pda.dialog.InputDialog;
import com.sanyang.pda.dialog.SelectPop;
import com.sanyang.pda.presenter.ConfirmStockPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.utils.Utils;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppActivityKey.CONFIRMSTOCKACTIVITY)
public class ConfirmStockActivity extends BaseMvpActivity<ConfirmStockPresenter> implements ConfirmStockContract.View {
    TakeStockBean takeStockBean;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.title)
    FrameLayout title;
    @BindView(R.id.tv_kucun_amount)
    TextView tvKucunAmount;
    @BindView(R.id.tv_shiji)
    TextView tvShiji;
    @BindView(R.id.tv_kuwei)
    TextView tvKuwei;
    @BindView(R.id.tv_tray)
    TextView tvTray;
    @BindView(R.id.tv_goods_code)
    TextView tvGoodsCode;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_guige)
    TextView tvGuige;
    @BindView(R.id.tv_pici)
    TextView tvPici;
    @BindView(R.id.tv_time_start)
    TextView tvTimeStart;
    @BindView(R.id.tv_product_time)
    TextView tvProductTime;
    @BindView(R.id.tv_admire_date)
    TextView tvAdmireDate;
    @BindView(R.id.bt_confirm)
    Button btConfirm;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.bt_scan)
    ScanButView btScan;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    private String storagebinId, taskNo;
    ByBarcodeBean byBarcodeBean;

    @Override
    public ConfirmStockPresenter getPresenter() {
        return new ConfirmStockPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_confirmstock;
    }

    @Override
    public void initView() {
        if (takeStockBean != null) {
            tvTitle.setText(getString(R.string.stock_confirm));
            tvAdmireDate.setText(takeStockBean.getExpiringTime());
            tvGoodsCode.setText(takeStockBean.getArticleId());
            tvGuige.setText(takeStockBean.getSpec());
            tvName.setText(takeStockBean.getArticleName());
            tvTimeStart.setText(takeStockBean.getLastCountTime());
            tvProductTime.setText(takeStockBean.getProductTime());
            tvKuwei.setText(takeStockBean.getStoragebinId());
            tvTray.setText(takeStockBean.getPallet());
            tvPici.setText(takeStockBean.getBatchNo());
            tvKucunAmount.setText(getString(R.string.inventory_number) + "     " + takeStockBean.getStockNum() + Utils.getArticleUnit(mContext, takeStockBean.getArticleUnit()));
            tvShiji.setText(takeStockBean.getActualNum()+"");
            tvUnit.setText( Utils.getArticleUnit(mContext, takeStockBean.getArticleUnit()));
        } else {
            tvTitle.setText(getString(R.string.add));
            tvKuwei.setText(storagebinId);
        }
    }

    @Override
    public void initLinsenterner() {
        btScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                switch (scanBean.getType()) {
                    case -1://商品
                        //tvGoods.setText(scanBean.getCode());
                        presenter.selectArticleInfoByBarcode(scanBean.getCode(), taskNo);
                        break;
                }
            }
        });
    }

    @Override
    public void getData() {
        takeStockBean = (TakeStockBean) getIntent().getExtras().getSerializable(StringConfig.BEAN);
        storagebinId = getIntent().getExtras().getString(StringConfig.STORAGEBINID, "");
        taskNo = getIntent().getExtras().getString(StringConfig.TASKNO, "");
    }


    @OnClick(R.id.iv_back)
    public void onIvBackClicked() {
        finishActivity();
    }

    @OnClick(R.id.tv_cancel)
    public void onTvCancelClicked() {
        finishActivity();
    }

    @OnClick(R.id.bt_confirm)
    public void onBtConfirmClicked() {
        presenter.stockAdd(takeStockBean);
    }
    List<SelectBean> selectBeanList=new ArrayList<>();
    @Override
    public void setGoodsList(List<ByBarcodeBean> list) {
        if (list.size() == 0){
            showMessage("未查询到商品信息");
            return;
        }
        if (list.size() == 1){
            this.byBarcodeBean = list.get(0);
            tvGoodsCode.setText(byBarcodeBean.getBarcode());
            tvGuige.setText(byBarcodeBean.getSpec());
            tvName.setText(byBarcodeBean.getArticleName());
            tvUnit.setText(Utils.getArticleUnit(mContext, byBarcodeBean.getArticleUnit()));
            tvKucunAmount.setText(getString(R.string.inventory_number) + "     " + 0 + Utils.getArticleUnit(mContext, byBarcodeBean.getArticleUnit()));
        }
        if (list.size() > 1){
            selectBeanList.clear();
            for (int i=0;i< list.size();i++){
                ByBarcodeBean byBarcodeBean=list.get(i);
                SelectBean selectBean=new SelectBean(byBarcodeBean.getArticleName(),byBarcodeBean.getArticleId());
                selectBean.setOther(byBarcodeBean.getSpec());
                selectBeanList.add(selectBean);
            }
            SelectPop selectPop=new SelectPop(mContext, new SelectPop.Onclick() {
                @Override
                public void onClick(SelectBean content) {
                    for (ByBarcodeBean itme: list){
                        if (itme.getArticleId().equals(content.getId())){
                            byBarcodeBean=itme;
                        }
                        tvGoodsCode.setText(byBarcodeBean.getBarcode());
                        tvGuige.setText(byBarcodeBean.getSpec());
                        tvName.setText(byBarcodeBean.getArticleName());
                        tvUnit.setText(Utils.getArticleUnit(mContext, byBarcodeBean.getArticleUnit()));
                    }
                }
            });
            selectPop.show(selectBeanList);
        }
    }

    @OnClick(R.id.tv_shiji)
    public void onViewClicked() {
        InputDialog inputDialog = new InputDialog(this, new InputDialog.OnDialogClick() {
            @Override
            public void onQueren(String content) {

                if (takeStockBean != null) {
                    try {
                       /* if (Double.parseDouble(content) > takeStockBean.getStockNum()){
                            showMessage(getString(R.string.real_quantity)+getString(R.string.no_greater)+getString(R.string.inventory_number));
                            return;
                        }*/
                        tvShiji.setText( content );
                        takeStockBean.setActualNum(Double.valueOf(content));
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                } else {
                    tvShiji.setText(content);
                }

            }
        });
        inputDialog.showNum();
    }
}
